#include "StdAfx.h"



#include "FileItem.h"

#include "../Stream/FileIO.h"
#include "FileFind.h"
#include "Common/System/Time.h"

namespace Common
{
//TODO: refactor this function
#ifdef SUPPORT_DEVICE_FILE
bool IsDeviceName(LPCTSTR n);
#endif
namespace FileSystem
{
namespace File
{



CFileInfoBase::CFileInfoBase(const CFileInfoBase & obj) : 
	Attributes(obj.Attributes), AccessTime(obj.AccessTime),
		ModificationTime(obj.ModificationTime), CreationTime(obj.CreationTime),
		IsDevice(obj.IsDevice), Size(obj.Size)
	{		
	}

	CFileInfoBase & CFileInfoBase::operator=(const CFileInfoBase & obj)
	{
		if(this == &obj)
			return *this;
		this->Attributes = obj.Attributes;
		this->AccessTime = obj.AccessTime;
		this->ModificationTime = obj.ModificationTime;
		this->CreationTime = obj.CreationTime;
		this->IsDevice = obj.IsDevice;
		this->Size = obj.Size;
		return *this;
	}

static const TCHAR kDot = TEXT('.');

CFileInfo::CFileInfo(const CFileInfo& obj) : CFileInfoBase(obj), Name(obj.Name)
{
}
 CFileInfo& CFileInfo::operator=(const CFileInfo& obj)
 {
	 if(this == &obj)
		 return *this;
	 CFileInfoBase::operator=(obj);
	 this->Name = obj.Name;
	 return *this;
 }

 void CFileInfoBase::ResetTimeToCurrent()
 {
	 Common::System::Time::GetCurUtcFileTime(ModificationTime);
	 CreationTime = AccessTime = ModificationTime;
 }

bool CFileInfo::IsDots() const
{
  if (!IsDir() || Name.IsEmpty())
    return false;
  if (Name[0] != kDot)
    return false;
  return Name.GetLength() == 1 || (Name[1] == kDot && Name.GetLength() == 2);
}

#define MY_CLEAR_FILETIME(ft) ft.dwLowDateTime = ft.dwHighDateTime = 0;

void CFileInfoBase::Clear()
{
  Size = 0;  
  MY_CLEAR_FILETIME(CreationTime);
  MY_CLEAR_FILETIME(AccessTime);
  MY_CLEAR_FILETIME(ModificationTime);
  Attributes = 0;
}
  
bool CFileInfo::Find(LPCTSTR wildcard)
{
  #ifdef SUPPORT_DEVICE_FILE
  if (IsDeviceName(wildcard))
  {
    Clear();
    IsDevice = true;
    Common::IO::CInFile inFile;
    if (!inFile.Open(wildcard))
      return false;
    Name = wildcard + 4;
    if (inFile.LengthDefined)
      Size = inFile.Length;
    return true;
  }
  #endif
  Find::CFindFile finder;
  return finder.FindFirst(wildcard, this);
}

} //File
} //FileSystem
} //Common